<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Java文档搜索</title>
</head>
<body>
    <!--1.搜索框加搜索按钮
        2.显示搜索结果-->
    <div class="container">
        <div class="header">
            <input type="text">
            <button id="search-btn">搜索</button>
        </div>

        <div class="result">
            <!--<div class="item">
                <a href="#">我是标题</a>
                <div class="desc">我是描述. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet asperiores, atque cum cumque dolores dolorum enim eos fugiat illum inventore iure natus nostrum, omnis quae quis quo quos similique tempore!</div>
                <div class="url">http://www.baidu.com</div>
            </div>
            <div class="item">
                <a href="#">我是标题</a>
                <div class="desc">我是描述. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet asperiores, atque cum cumque dolores dolorum enim eos fugiat illum inventore iure natus nostrum, omnis quae quis quo quos similique tempore!</div>
                <div class="url">http://www.baidu.com</div>
            </div>
            <div class="item">
                <a href="#">我是标题</a>
                <div class="desc">我是描述. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet asperiores, atque cum cumque dolores dolorum enim eos fugiat illum inventore iure natus nostrum, omnis quae quis quo quos similique tempore!</div>
                <div class="url">http://www.baidu.com</div>
            </div>
            <div class="item">
                <a href="#">我是标题</a>
                <div class="desc">我是描述. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet asperiores, atque cum cumque dolores dolorum enim eos fugiat illum inventore iure natus nostrum, omnis quae quis quo quos similique tempore!</div>
                <div class="url">http://www.baidu.com</div>
            </div>
        </div>-->
        </div>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        html,body{
            height: 100%;
            background-image: url(preview.jpg);
            background-repeat: no-repeat;
            background-position: center;
            background-size: cover;
        }

        .container{
            width: 1200px;
            height: 100%;
            margin: 0 auto;
            background-color: rgba(255,255,255,0.8);
            border-radius: 10px;
            padding: 30px;
            overflow: auto;
        }

        .header{
            width: 100%;
            height: 50px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header>input{
            width: 960px;
            height: 50px;
            font-size: 22px;
            line-height: 50px;
            padding-left: 10px;
            border-radius: 10px;
        }
        .header>button{
            width: 100px;
            height: 50px;
            background-color: dodgerblue;
            color: azure;
            font-size: 22px;
            line-height: 55px;
            border-radius: 10px;
            border: none;
        }
        .header>button:active{
            background: gray;
        }

        .result .count {
            color: gray;
            margin-top:10px;
        }

        .item{
            width: 100%;
            margin-top: 30px;
        }
        .item a{
            display: block;
            height: 40px;
            font-size: 22px;
            line-height: 40px;
            font-weight: 700;
            color: darkblue;
        }

        .item .desc{
            font-size: 18px;
        }
        .item .url{
            font-size: 18px;
            color: darkseagreen;
        }
        .item .desc i{
            color: red;
            font-style: normal;
        }
    </style>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<!--    <script src="js/jquery.js"></script>-->
    <script>
        let button = document.querySelector("#search-btn");
        button.onclick = function (){
            let input = document.querySelector(".header input");
            let query = input.value;
            console.log("query"+query);
            $.ajax({
                type:"GET",
                url:"searcher?query="+query,
                success: function (data , status) {
                    console.log(data);
                    buildResult(data);
                }
            });
        }
        function buildResult(data){
            //通过这个函数，来把响应数据构造成页面内容。
            //遍历data中的元素，针对每一个元素创建div.item,把标题url，描述都构造好
            //把div.item都加入到itemResults中，基于DOM展开
            let result = document.querySelector('.result');
            result.innerHTML='';
            //构造一个div显示结果的个数
            let countDiv = document.createElement('div');
            countDiv.innerHTML = '当前找到' + data.length+'个结果';
            countDiv.className='count';
            result.appendChild(countDiv);
            for(let item of data){
                let itemDiv = document.createElement('div');
                itemDiv.className = 'item';

                //构造标题
                let title = document.createElement('a');
                title.innerHTML = item.title
                title.href = item.url;
                title.target = '_blank';
                itemDiv.appendChild(title);

                //构造描述
                let desc = document.createElement('div');
                desc.className = 'desc';
                desc.innerHTML = item.desc;
                itemDiv.appendChild(desc);

                let url = document.createElement('div');
                url.className='url';
                url.innerHTML = item.url;
                itemDiv.appendChild(url);

                result.appendChild(itemDiv);
            }
        }
    </script>
    </div>
</body>
</html>